<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Section 4.6.3: Find the fastest mixing Markov chain on a graph</title>
<link rel="canonical" href="/Users/mcgrant/Projects/CVX/examples/cvxbook/Ch04_cvx_opt_probs/html/fastest_mixing_MC.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Section 4.6.3: Find the fastest mixing Markov chain on a graph</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
Plots
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Boyd &amp; Vandenberghe "Convex Optimization"</span>
<span class="comment">% Jo&euml;lle Skaf - 09/26/05</span>
<span class="comment">%</span>
<span class="comment">% The 'fastest mixing Markov chain problem' is to find a transition</span>
<span class="comment">% probability matrix P on a graph E that minimizes the mixing rate r, where</span>
<span class="comment">% r = max{ lambda_2, -lambda_n } with lambda_1&gt;=...&gt;=lambda_n being the</span>
<span class="comment">% eigenvalues of P.</span>

<span class="comment">% Generate input data</span>
n = 5;
E = [0 1 0 1 1; <span class="keyword">...</span>
     1 0 1 0 1; <span class="keyword">...</span>
     0 1 0 1 1; <span class="keyword">...</span>
     1 0 1 0 1; <span class="keyword">...</span>
     1 1 1 1 0];

<span class="comment">% Create and solve model</span>
cvx_begin
    variable <span class="string">P(n,n)</span> <span class="string">symmetric</span>
    minimize(norm(P - (1/n)*ones(n)))
    P*ones(n,1) == ones(n,1);
    P &gt;= 0;
    P(E==0) == 0;
cvx_end
e = flipud(eig(P));
r = max(e(2), -e(n));

<span class="comment">% Display results</span>
disp(<span class="string">'------------------------------------------------------------------------'</span>);
disp(<span class="string">'The transition probability matrix of the optimal Markov chain is: '</span>);
disp(P);
disp(<span class="string">'The optimal mixing rate is: '</span>);
disp(r);
</pre>
<a id="output"></a>
<pre class="codeoutput">
 
Calling SDPT3 4.0: 75 variables, 9 equality constraints
   For improved efficiency, SDPT3 is solving the dual problem.
------------------------------------------------------------

 num. of constraints =  9
 dim. of sdp    var  = 10,   num. of sdp  blk  =  1
 dim. of linear var  = 15
 dim. of free   var  =  5
 12 linear variables from unrestricted variable.
 *** convert ublk to lblk
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
   HKM      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
 0|0.000|0.000|5.1e+01|2.6e+01|5.3e+03| 1.600000e+01  0.000000e+00| 0:0:00| chol  1  1 
 1|0.992|0.987|3.9e-01|4.3e-01|6.4e+01| 2.312263e+01 -9.647124e+00| 0:0:00| chol  1  1 
 2|1.000|1.000|1.1e-06|1.0e-02|4.7e+00| 2.706490e+00 -1.936915e+00| 0:0:00| chol  1  1 
 3|0.995|0.724|3.8e-08|3.5e-03|7.1e-01|-1.054827e-01 -7.979247e-01| 0:0:00| chol  1  1 
 4|0.745|0.960|7.1e-09|2.4e-04|2.4e-01|-6.247315e-01 -8.670599e-01| 0:0:00| chol  1  1 
 5|0.982|0.975|2.2e-10|1.6e-05|5.4e-03|-7.468838e-01 -7.522799e-01| 0:0:00| chol  1  1 
 6|0.989|0.989|3.5e-11|1.2e-06|6.1e-05|-7.499647e-01 -7.500254e-01| 0:0:00| chol  1  1 
 7|0.989|0.989|1.1e-11|1.5e-06|7.3e-07|-7.499996e-01 -7.500003e-01| 0:0:00| chol  2  2 
 8|0.997|0.989|5.7e-13|1.8e-08|9.1e-09|-7.500000e-01 -7.500000e-01| 0:0:00| chol  2  2 
 9|0.998|0.989|1.7e-13|2.3e-10|1.1e-10|-7.500000e-01 -7.500000e-01| 0:0:00|
  stop: max(relative gap, infeasibilities) &lt; 1.49e-08
-------------------------------------------------------------------
 number of iterations   =  9
 primal objective value = -7.50000000e-01
 dual   objective value = -7.50000000e-01
 gap := trace(XZ)       = 1.07e-10
 relative gap           = 4.27e-11
 actual relative gap    = 4.22e-11
 rel. primal infeas (scaled problem)   = 1.71e-13
 rel. dual     "        "       "      = 2.28e-10
 rel. primal infeas (unscaled problem) = 0.00e+00
 rel. dual     "        "       "      = 0.00e+00
 norm(X), norm(y), norm(Z) = 1.1e+00, 8.3e-01, 2.8e+00
 norm(A), norm(b), norm(C) = 9.1e+00, 2.0e+00, 2.3e+00
 Total CPU time (secs)  = 0.25  
 CPU time per iteration = 0.03  
 termination code       =  0
 DIMACS: 1.7e-13  0.0e+00  3.8e-10  0.0e+00  4.2e-11  4.3e-11
-------------------------------------------------------------------
 
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +0.75
 
------------------------------------------------------------------------
The transition probability matrix of the optimal Markov chain is: 
         0    0.3750         0    0.3750    0.2500
    0.3750         0    0.3750         0    0.2500
         0    0.3750         0    0.3750    0.2500
    0.3750         0    0.3750         0    0.2500
    0.2500    0.2500    0.2500    0.2500         0

The optimal mixing rate is: 
    0.7500

</pre>
</div>
</body>
</html>